﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace Scrt
{
    /// <summary>
    /// 软件注册类
    /// </summary>
    public class SoftwareRegister
    {
        /// <summary>
        /// License所在的文件名
        /// </summary>
        private const string LICENSEFILENAME = "License.bin";
        /// <summary>
        /// 注册码随机次数
        /// </summary>
        private const int RND_TIMES = 2;
        /// <summary>
        /// License文件路径
        /// </summary>
        private string LicensePath;
        /// <summary>
        /// 主页网址
        /// </summary>
        private const string HOME_PAGE = "https://shop148743344.taobao.com/?spm=a230r.7195193.1997079397.2.gRMsB5";
        /// <summary>
        /// Host标识码
        /// </summary>
        public string HostIdentify{get;protected set;}
        /// <summary>
        /// 账户名称
        /// </summary>
        private string UserID;
        /// <summary>
        /// 软件名称
        /// </summary>
        public string SoftwareName { get; protected set; }
        /// <summary>
        /// 注册结果
        /// </summary>
        private RegisterResult _regResut;
        /// <summary>
        /// 注册结果
        /// </summary>
        public RegisterResult RegResult
        {
            get
            {
                InitRegState();
                return _regResut;
            }
        }
        /// <summary>
        /// 注册类构造函数
        /// 软件使用
        /// </summary>
        /// <param name="softwareName">软件名称枚举</param>
        public SoftwareRegister(SoftNameDef softwareName)
        {
            string BaseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + softwareName + "\\";
            this.SoftwareName = softwareName.ToString();
            Funcs.CreateFolder(BaseDir);
            LicensePath = BaseDir + LICENSEFILENAME;
            HostIdentify = Funcs.GetHostDiskVolumeStr();
        }
        /// <summary>
        /// 注册类构造函数
        /// 注册机使用
        /// </summary>
        /// <param name="softwareName">字符串软件名</param>
        public SoftwareRegister(string softwareName)
        {
            this.SoftwareName = softwareName;
        }
        /// <summary>
        /// 获取注册码
        /// 注册机使用
        /// </summary>
        /// <param name="hostIdentify">机器码</param>
        /// <param name="userID">用户账户</param>
        /// <returns>注册码字符串</returns>
        public string GetRegStr(string hostIdentify, string userID)
        {
            string str = hostIdentify + userID + this.SoftwareName;
            str = Funcs.GetMD5Hash(str, 32);
            str = Funcs.RandmStr(str, RND_TIMES);
            return str;
        }
        /// <summary>
        /// 注册软件
        /// 软件使用
        /// </summary>
        /// <param name="userID">用户账户</param>
        /// <param name="regStr">注册码</param>
        public void RegisterSoftware(string userID, string regStr)
        {
            string str = string.Format("{0}_{1}", userID, regStr);
            Funcs.SaveLicense(LicensePath,str);
        }
        /// <summary>
        /// 访问主页
        /// </summary>
        public void ShowHomePage()
        {
            System.Diagnostics.Process.Start(HOME_PAGE);
        }
        /// <summary>
        /// 初始化注册结果
        /// </summary>
        private void InitRegState()
        {
            string str = Funcs.GetLicense(LicensePath);
            string[] ArrStr = str.Split('_');
            if (ArrStr.Length != 2)
            {
                _regResut = RegisterResult.FAILED;
                return;
            }
            this.UserID = ArrStr[0];
            string RegStr = ArrStr[1];
            if (IsValidRegStr(RegStr))
            {
                _regResut = RegisterResult.SUCCEED;
            }
            else
            {
                _regResut = RegisterResult.FAILED;
            }
        }
        /// <summary>
        /// 判断是否为有效注册码
        /// </summary>
        /// <param name="regStr">注册码</param>
        /// <returns>true：有效</returns>
        private bool IsValidRegStr(string regStr)
        {
            string str = Funcs.DeRandmStr(regStr, RND_TIMES);
            string HostRegStr = GetHostRegStr();
            if (0 == string.Compare(str, HostRegStr))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取机器码
        /// </summary>
        /// <returns></returns>
        private string GetHostRegStr()
        {
            string str = this.HostIdentify + this.UserID + this.SoftwareName;
            str = Funcs.GetMD5Hash(str, 32);
            return str;
        }

    }
}
